
class spotoBin{

public:
  vector <point> p;

  double deltaHI;
  double deltaLO;

  spotoBin()
  {
    deltaHI=deltaLO=0;
  }
  
  // from a sorted array of points pglo
  // import only those inside the range
  spotoBin(double lo, double hi, vector <point> pglo)
  {
    deltaLO=lo;
    deltaHI=hi;
    
    vector<point>::iterator pIterLo = std::lower_bound(pglo.begin(), pglo.end(), lo);
    vector<point>::iterator pIterHi = std::lower_bound(pglo.begin(), pglo.end(), hi);

    for(vector<point>::iterator pi = pIterLo; pi < pIterHi; pi++)
      p.push_back(*pi);
  }

  void print()
  {
    cout << "bin> " << deltaLO << " " << deltaHI << " " << p.size() << endl;
  }
  
  void printPoints()
  {
    for(vector<point>::iterator pi = p.begin(); pi < p.end(); pi++)
      pi->print();
  }
  
  void addPoint(point pp)
  {
    p.push_back(pp);
  }

  spotoBin split()
  {
    spotoBin a((deltaLO+deltaHI)/2, deltaHI, p);
    vector<point>::iterator pIterHi = std::lower_bound(p.begin(), p.end(), deltaHI);
    deltaHI=(deltaLO+deltaHI)/2;
    vector<point>::iterator pIterLo = std::lower_bound(p.begin(), p.end(), deltaHI);

    p.erase(pIterLo,pIterHi);
    
    return a;
  }

  long getNpoints()
  {
    return p.size();
  }

  point getLeftMost()
  {
    double a=1000;
    vector<point>::iterator ppi;
    long N=p.size();

    for(vector<point>::iterator pi = p.begin(); pi < p.end(); pi++)
      if (pi->x<a)
	{
	  ppi=pi; a=pi->x;
	}

    return *ppi;
  }

  bool operator < (const spotoBin &b)
  {
    if (this->deltaHI <= b.deltaLO) return true;
    else return false;
  }
};
